﻿using System;
using System.Threading;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using Newtonsoft.Json.Linq;

namespace Test_API
{
    class Program
    {
        private static void Main(string[] args)
        {
            string accessToken = String.Empty, cid = String.Empty, name = String.Empty;

            string inputPath = @"C:\Sandbox\L2P-API\inputs.json";
            if (File.Exists(inputPath.Replace("/","\\")))
            {
                JArray inputSet = JArray.Parse(File.ReadAllText(inputPath));
                foreach (var item in inputSet)
                {
                    name = (string)item["name"];
                    accessToken = (string)item["accessToken"];
                    cid = (string)item["cid"];
                    
                    Program.singleCallSingleUser(accessToken, cid, name);
                }
            }
            else
            {
                do
                {
                    Console.WriteLine("\n\nPlease provide inputs in this format and press enter: Name CID ACCESS-TOKEN");
                    Console.WriteLine("Name: ");
                    name = Console.ReadLine();
                    Console.Write("Give cid: ");
                    cid = Console.ReadLine();
                    Console.Write("Give access-token: ");
                    accessToken = Console.ReadLine();

                    Program.singleCallSingleUser(accessToken, cid, name);
                    
                    Console.Clear();
                    Console.WriteLine("\n\nDo you want to continue? Type y/n and press enter.");
                } while (Console.ReadLine() == "y");
            }
        }

        private static void singleCallSingleUser(string accessToken, string cid, string name)
        {
            string projectFolder = "c:/sandbox/L2P-API", localFile = String.Empty;
            if (!Directory.Exists(projectFolder))
            {
                Directory.CreateDirectory(projectFolder);
                Console.WriteLine("Project folder created: {0}\n============", projectFolder);
            }
            localFile = new DirectoryInfo(projectFolder).GetFiles().ToList().randomElement().FullName;

            List<LogEntry> allModuleLogEntries = new List<LogEntry>();

            allModuleLogEntries.AddRange(new InitAndPingTest(accessToken).start());
            //allModuleLogEntries.AddRange(new AnnouncementTest(accessToken, cid).start(localFile));
            //allModuleLogEntries.AddRange(new EmailTest(accessToken, cid).start(localFile));
            //allModuleLogEntries.AddRange(new HyperlinkTest(accessToken, cid).start());
            allModuleLogEntries.AddRange(new LearningMaterialTest(accessToken, cid, projectFolder).start(localFile));
            allModuleLogEntries.AddRange(new MediaLibraryTest(accessToken, cid, projectFolder).start(localFile));
            allModuleLogEntries.AddRange(new SharedDocumentTest(accessToken, cid, projectFolder).start(localFile));
            //allModuleLogEntries.AddRange(new AssessmentTest(accessToken, cid).start(localFile));
            //allModuleLogEntries.AddRange(new WikiTest(accessToken, cid).start());
            //allModuleLogEntries.AddRange(new DiscussionForumTest(accessToken, cid).start());
            //allModuleLogEntries.AddRange(new GroupWorkSpaceTest(accessToken, cid).start());

            Program.writeToCSV(allModuleLogEntries, projectFolder, name);
        }

        private static void writeToCSV(List<LogEntry> logList, string projectFolder, string name)
        {
            Console.WriteLine("Print logentries with {verdict=false} begin\n============");
            List<string> csv = new List<string>();
            string header = "elapsedTime (ms), module, methodName, inputs, expectedResult, receivedData, verdict";
            csv.Add(header);

            foreach (var item in logList)
            {
                string singleLog = item.ToString();
                csv.Add(singleLog);
            }
            string fileName = projectFolder + string.Format("/{0}_{1:dd-MMM_HH.mm.ss}_{2}.csv", name, DateTime.Now, ExtenstionMethods.randomNumber(250));
            File.WriteAllLines(fileName, csv.ToArray());
            Console.WriteLine("Saved log file in the location:\n============\n{0}\n", fileName);
            
            Console.WriteLine("Finished API call for '{0}'. Please press ENTER to continue;", name);
            Console.ReadLine();
            Console.Clear();
        }
    }
}